package com.glory.dgcpcpda.activity.mfg;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.EqpDataAcquistionAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpKeepBean;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.entity.EqpDataAcquisitionBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;
import butterknife.OnClick;

public class Eqp_DataAcquisitionActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener{

    @BindView(R.id.dataAcq_recycler)
    RecyclerView dataAcq_recycler;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    // 定义下拉列表需要显示的文本数组
    private List<String> starArray = new ArrayList<>();
    private EqpDataAcquistionAdapter eqpDataAcqAdapter;
    private ArrayAdapter<String> starAdapter;
    private List<EqpDataAcquisitionBean.EDCSETLISTDTO> edcsetlist;
    private String EDCSETNAME;
    private String sheetName;
    List<EqpDataAcquisitionBean.EDCSETLISTDTO.EDCSETITEMLINELISTDTO> edcsetitemlinelist;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_data_acquisition;
    }


    @Override
    protected void initView() {

        setMainLayout(llMain);

        Bundle bundle = getIntent().getExtras();
        sheetName = bundle.getString("sheetName");

        LinearLayoutManager llm = new LinearLayoutManager(Eqp_DataAcquisitionActivity.this);
        dataAcq_recycler.setLayoutManager(llm);
        dataAcq_recycler.setItemAnimator(new DefaultItemAnimator());
        dataAcq_recycler.addItemDecoration(new DividerItemDecoration(Eqp_DataAcquisitionActivity.this, 1));

        eqpDataAcqAdapter = new EqpDataAcquistionAdapter(null,true);
        eqpDataAcqAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpDataAcqAdapter.isFirstOnly(true);
        dataAcq_recycler.setAdapter(eqpDataAcqAdapter);

    }


    @Override
    protected void initData() {


        FreeUI_EntityCallBack<BaseResponseBean<EqpDataAcquisitionBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpDataAcquisitionBean>>
                (new TypeToken<BaseResponseBean<EqpDataAcquisitionBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpDataAcquisitionBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpDataAcquisitionBean bean = response.body().getResponse().getBody();
                    if (bean.getEDCSETLIST().size() > 0) {

                        for (EqpDataAcquisitionBean.EDCSETLISTDTO data : bean.getEDCSETLIST()) {
                            starArray.add(data.getEDCSETNAME());
                        }

//                        starAdapter.notifyDataSetChanged();
                        initSpinnerForDropdown();
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpDataAcquisitionBean>> response) {
                super.onError(response);
                loadError(response.getException(), "TRANSFERMES");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "QUERYBYEDCNAME");
        map.addProperty("EDCTYPE", "ITEM");
        map.addProperty("EDCSETNAME", sheetName);
        NetUtils.requestPostNet("CPC.EDCITEMSET", "TRANSFERMES", this, map, callBack);


    }


    @OnClick({R.id.tvDataEcq_submit})
    public void onViewClicked(View view) {

        switch (view.getId()){

            case R.id.tvDataEcq_submit:
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {

                        submit();
                    }
                },"确认提交？");
                break;

        }

    }


    private void submit() {

        FreeUI_EntityCallBack<BaseResponseBean<EqpKeepBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>
                (new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(Eqp_DataAcquisitionActivity.this, "操作成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "TRANSFERMES");
            }
        };

        List<EqpDataAcquisitionBean.EDCSETLISTDTO.EDCSETITEMLINELISTDTO> data = eqpDataAcqAdapter.getData();
        JsonObject body = new JsonObject();
        body.addProperty("ACTIONTYPE", "SUBMIT");
        JsonArray edcsetList = new JsonArray();
        JsonObject edcset = new JsonObject();
        edcset.addProperty("EDCSETNAME", EDCSETNAME);
        edcset.addProperty("EDCSETDESCRIPTION", edcsetlist.get(0).getEDCSETDESCRIPTION());
        edcset.addProperty("EDCTYPE", edcsetlist.get(0).getEDCTYPE());
        JsonArray edcsetItemLineList = new JsonArray();
        for (int i = 0;i<edcsetitemlinelist.size();i++){
            JsonArray xedcsetList = new JsonArray();
            JsonObject itemLine = new JsonObject();
            itemLine.addProperty("OBJECTRRN", edcsetitemlinelist.get(i).getOBJECTRRN());
            itemLine.addProperty("NAME", edcsetitemlinelist.get(i).getNAME());
            itemLine.addProperty("DESCRIPTION", edcsetitemlinelist.get(i).getDESCRIPTION());
            itemLine.addProperty("SUBGROUPSIZE", edcsetitemlinelist.get(i).getSUBGROUPSIZE());
            itemLine.addProperty("ITEM", edcsetitemlinelist.get(i).getITEM());
            itemLine.addProperty("USL", edcsetitemlinelist.get(i).getUSL());
            itemLine.addProperty("LSL", edcsetitemlinelist.get(i).getLSL());
            itemLine.addProperty("ISMANDATORY", edcsetitemlinelist.get(i).getISMANDATORY());
            for (int j = 0; j<data.size();j++){
                if (data.get(j).getNAME().equals(edcsetitemlinelist.get(i).getNAME())){
                    xedcsetList.add(data.get(j).getVALUE());
                }
            }
            itemLine.add("XEdcSet",xedcsetList);

            edcsetItemLineList.add(itemLine);
        }
        edcset.add("EDCSETITEMLINELIST", edcsetItemLineList);
        edcsetList.add(edcset);

        body.add("EDCSETLIST", edcsetList);
        NetUtils.requestPostNet("CPC.EDCITEMSET", "TRANSFERMES", this, body, callBack);

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    // 初始化下拉模式的列表框
    private void initSpinnerForDropdown() {
        // 声明一个下拉列表的数组适配器
        starAdapter = new ArrayAdapter<String>(this,
                R.layout.item_eqp_data_acquisition, starArray);
        // 从布局文件中获取名叫sp_dropdown的下拉框
        Spinner sp_dropdown = findViewById(R.id.sp_dataName);
        // 设置下拉框的标题。对话框模式才显示标题，下拉模式不显示标题
//        sp_dropdown.setPrompt("请选择行星");
        sp_dropdown.setAdapter(starAdapter); // 设置下拉框的数组适配器
        sp_dropdown.setSelection(0); // 设置下拉框默认显示第一项
        // 给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp_dropdown.setOnItemSelectedListener(new MySelectedListener());
    }




    // 定义下拉列表需要显示的文本数组
//    private String[] starArray = {"示例1", "示例2", "示例3"};
    // 定义一个选择监听器，它实现了接口OnItemSelectedListener
    class MySelectedListener implements AdapterView.OnItemSelectedListener {
        // 选择事件的处理方法，其中arg2代表选择项的序号
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//            Toast.makeText(Eqp_DataAcquisitionActivity.this, "您选择的是" + starArray.get(arg2),
//                    Toast.LENGTH_LONG).show();

            FreeUI_EntityCallBack<BaseResponseBean<EqpDataAcquisitionBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpDataAcquisitionBean>>
                    (new TypeToken<BaseResponseBean<EqpDataAcquisitionBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpDataAcquisitionBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                        EqpDataAcquisitionBean bean = response.body().getResponse().getBody();
                        if (bean.getEDCSETLIST().size() > 0) {

                            edcsetlist.clear();
                            edcsetlist = bean.getEDCSETLIST();
                            EDCSETNAME = starArray.get(arg2);

                            List<EqpDataAcquisitionBean.EDCSETLISTDTO.EDCSETITEMLINELISTDTO> adapterList = new ArrayList<>();
                            edcsetitemlinelist = bean.getEDCSETLIST().get(0).getEDCSETITEMLINELIST();
                            for (int i = 0;i<edcsetitemlinelist.size();i++){
                                for (int j=0;j<edcsetitemlinelist.get(i).getSUBGROUPSIZE();j++){
                                    adapterList.add(edcsetitemlinelist.get(i));
                                }
                            }
//                            adapterList.addAll(edcsetlist.get(0).getEDCSETITEMLINELIST());

                            eqpDataAcqAdapter.setNewData(adapterList);
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<EqpDataAcquisitionBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "TRANSFERMES");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "QUERYBYEDCNAME");
            map.addProperty("EDCTYPE", "ITEM");
            map.addProperty("EDCSETNAME", starArray.get(arg2));
            NetUtils.requestPostNet("CPC.EDCITEMSET", "TRANSFERMES", this, map, callBack);


        }

        // 未选择时的处理方法，通常无需关注
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }


}